import {cloud} from "@/framework/utils/request";
import {getToken} from "@/framework/utils/auth";
import systemApi from "@/api/investment/system";

export default {
  data() {
    return {
      MX_fileList: [],
      MX_uploadLoading: false
    }
  },
  methods: {
    $_beforeHandleSuccess(file, fileInfo) {
      //fileTypes: 例如['png', 'jpg', 'jpeg'],['doc', 'docx', 'xlsx', 'xls', 'ppt', 'txt', 'pdf'],["mp4", "avi", "mpeg"]
      if (!file) return
      if (!fileInfo.fileSize) fileInfo.fileSize = 5
      if (!fileInfo.fileTypes) fileInfo.fileTypes = []
      let fileSizeF = file.size / 1024 / 1024 < fileInfo.fileSize;
      const s = fileInfo.fileTypes.filter((el) => {
        return file.name.toLowerCase().lastIndexOf(el) > 0;
      });
      if (!!fileInfo.fileTypes.length && s.length == 0) {
        this.$message.error("上传文件格式不正确");
        return false;
      }
      if (!fileSizeF) {
        this.$message.error("上传文件大小不能超过" + fileInfo.fileSize + "MB!");
        return false;
      }
      if (file.name.length > 50) {
        this.$message.error("文件名称过长，建议长度在50字符内。");
        return false;
      }
      var flag = false;
      this.MX_fileList.forEach((element) => {
        if (file.name === element.name) {
          flag = true;
          return flag;
        }
      });
      if (flag) {
        this.$message.error("上传文件重复！");
        return false;
      }
      return true;
    },
    $_handleExceed(files, fileList) {
      this.$message.warning("已超过文件上传个数限制！");
    },
    $_remove(file, fileList) {
      this.MX_fileList = fileList;
    },
    $_uploadFile(data, fileInfo){
      this.MX_uploadLoading = true
      let query = new FormData()
      query.append('secretFlag', 'N')
      query.append('fileBucket', 'investment')
      query.append('file', data.file)

      const arr = data.file.name.split('.')
      const isVideo = ["mp4", "avi", "mpeg"].includes(arr[arr.length-1])
      const apiFun = !!isVideo ? 'uploadVideo' : 'uploadFile'
      systemApi[apiFun](query).then(res => {
        this.MX_uploadLoading = false
        this.$dtMessage({
          title: "成功",
          message: "上传成功",
          type: "success",
          duration: 2000,
        });
        if (!fileInfo.limit) this.MX_fileList = []
        if (!!fileInfo.callBack) fileInfo.callBack(res.data)
      }).catch(e => {
        this.MX_uploadLoading = false
      })
    }
  }
}
